/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import agh.rozproszony.model.Message;
import agh.rozproszony.model.User;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.views.AbstractView;

/**
 *
 * @author Marcin_PC
 */
public class MessageTable extends AbstractTableModel {

    private final static Object[] columnNames = {"Osoba", "Tytuł ", "Tresc"};
    private List<MessageRow> rows;
    private final static int USER_IDX = 0;
    private final static int TITLE_IDX = 1;
    private final static int MESSAGE_IDX = 2;

    MessageTable() {
        this.rows = new ArrayList<MessageRow>();
    }

    @Override
    public int getRowCount() {
        if (rows == null) {
            return 0;
        }
        return rows.size();
    }
    
    public void Clear()
    {
        rows.clear();
        
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column].toString();
    }

    public MessageRow getRow(int id)
    {
        return rows.get(id);
        
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rows == null) {
            return null;
        }
        MessageRow row = (MessageRow) rows.get(rowIndex);
        switch (columnIndex) {
            case USER_IDX:
                return row.getPerson();
                //return row.getTempolaryPerson();
            case TITLE_IDX:
                return row.getTitle();
            case MESSAGE_IDX:
                return row.getMessage();
            default:
                return row;
        }
    }

    
    public void addMessage(List<Message> messages, List<User> usersList) {
        /*for (Message message : messages) {

            MessageRow messageRow = new MessageRow(message);
            rows.add(messageRow);
        
        }*/
        
        for(int i=0;i<messages.size();i++)
        {
            String my = usersList.get((messages.get(i).getSenderId())-1).getLogin();
            
            MessageRow messageRow = new MessageRow(messages.get(i),my);
            rows.add(messageRow);
            
        }

    }
    /*
    public void addMessage(Message message)
    {
        MessageRow row = new MessageRow();
        
        row.setTempolaryPerson(message.getSenderId());
        row.setMessage(message.getContent());
        row.setTitle(message.getTitle());
        
        rows.add(row);
        
        
    }*/
    /*
    public void setModelData(List<Message> messages) {
        rows.clear();
        for (Message message : messages) {
            MessageRow row = null;
            row.setTempolaryPerson(1);
            row.setTitle(message.getTitle());
            row.setMessage(message.getContent());

            rows.add(row);
        }
    }*/

    

    
}
